package com.zenjava.update.ui;

import com.zenjava.update.AppProfile;
import com.zenjava.update.UpdateHandler;
import com.zenjava.update.ZenUpdateManager;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

public class DownloadAndInstallUpdateService extends Service<Void>
{
    private AppProfile appProfile;
    private UpdateHandler updateHandler;

    public void downloadAndInstall(AppProfile appProfile, UpdateHandler updateHandler)
    {
        this.appProfile = appProfile;
        this.updateHandler = updateHandler;
        restart();
    }

    protected Task<Void> createTask()
    {
        final AppProfile finalAppProfile = appProfile;
        final UpdateHandler finalUpdateHandler = updateHandler;
        return new Task<Void>()
        {
            protected Void call() throws Exception
            {
                ZenUpdateManager zenUpdateManager = ZenUpdateManager.getInstance();
                zenUpdateManager.downloadAndInstallUpdate(finalAppProfile, updateHandler);
                return null;
            }
        };
    }

}
